package com.lmt.ymcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.lmt.ymcc.domain.KillActivity;
import com.lmt.ymcc.domain.KillCourse;
import com.lmt.ymcc.mapper.KillActivityMapper;
import com.lmt.ymcc.service.IKillActivityService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lmt.ymcc.service.IKillCourseService;
import com.lmt.ymcc.utils.AssertUtil;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AmazingXu
 * @since 2022-08-23
 */
@Service
public class KillActivityServiceImpl extends ServiceImpl<KillActivityMapper, KillActivity> implements IKillActivityService {


    @Autowired
    private IKillCourseService killCourseService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private RedissonClient redissonClient;
    /**
    * @Title: publish
    * @Description: 活动发布
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/8/23 19:38
    * @Parameters: [activityId]
    * @Return void
    */
    @Override
    @Transactional
    public void publish(Long activityId) {
        // 通过活动id查询活动是否有效
        KillActivity killActivity = selectById(activityId);
        AssertUtil.isNotNull(killActivity, "活动不存在");
        // 判断活动是否已发布
        boolean isWait = killActivity.getPublishStatus() == KillActivity.STATES_WAIT_PUBLISH;
        AssertUtil.isTrue(isWait, "活动暂时未发布");
        // 判断活动是否已经结束
        boolean before = killActivity.getEndTime().after(new Date());
        AssertUtil.isTrue(before, "活动已过期");
        Wrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("activity_id", activityId);
        killActivity.setPublishStatus(KillActivity.STATES_ONLINE);
        killActivity.setPublishTime(new Date());
        List<KillCourse> killCourses = killCourseService.selectList(wrapper);
        killCourses.stream().forEach(killCourse -> {
            String killActivityCourseKey = killActivity.getId() + ":" + killCourse.getId();
            RSemaphore semaphore = redissonClient.getSemaphore(killActivityCourseKey);
            boolean isPermits = semaphore.trySetPermits(killCourse.getKillCount());
            if (isPermits) {
                redisTemplate.opsForHash().put(
                        "activity:" + killActivity.getId(),
                        killCourse.getId().toString(),
                        killCourse
                );
                killCourse.setPublishStatus(killActivity.getPublishStatus());
                killCourse.setPublishTime(killActivity.getPublishTime());
                killCourseService.updateById(killCourse);
            }else {
                // 如果有一条没有添加成功，生成日志，人工处理这条信息
            }
        });
        updateById(killActivity);
    }
}
